import React, { Component } from "react";
// 一切皆组件
import { Link, Route, Switch } from "react-router-dom";

class Home extends Component {
  render() {
    return <div>这是home组件</div>;
  }
}

class About extends Component {
  render() {
    return <div>这是About组件</div>;
  }
}

class Mine extends Component {
  render() {
    return <div>这是Mine组件</div>;
  }
}

class Detail extends Component {
  render() {
    // console.log(this);
    // ?.可选链操作符
    // const obj = {
    //   a: 3,
    // };
    // console.log(obj.b?.c);
    // fn?.();

    // 二次解构
    const {
      params: { id },
    } = this.props.match;
    return <div>这是Detail组件 - {id}</div>;
  }
}

class App extends Component {
  render() {
    return (
      <>
        <h2>react-router-动态路由</h2>
        <ul>
          <li>
            <Link to="/home">首页</Link>
          </li>
          <li>
            <Link to="/about">关于页</Link>
          </li>
          <li>
            <Link to="/mine">我的页</Link>
          </li>
          <li>
            <Link to="/detail/123456">详情页</Link>
          </li>
          <li>
            <Link to="/detail/654321">详情页</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Route path="/home" component={Home}></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/mine" component={Mine}></Route>
          {/* 动态路由的做法和vue的做法是一样的 */}
          <Route path="/detail/:id" component={Detail}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
